import pandas as pd
df = pd.read_csv('merge.csv')
df['create_time'] = pd.to_datetime(df['create_time'])
df['年份'] = df['create_time'].dt.year
df['月份'] = df['create_time'].dt.month
df['季度'] = (df['月份'] - 1) // 3 + 1

# 按'发货仓库'、'年份'、'季度'和'产品类型'分组，并计算总发货吨数
grouped = df.groupby(['hplx', 'cppp'])['jz'].sum().reset_index()

# 对每个'发货仓库'、'年份'和'季度'的组合进行排序，并选取前三个产品类型
top_products_per_warehouse_quarter = (grouped
                                      .groupby(['hplx'])
                                      .apply(lambda x: x.sort_values(by='jz', ascending=False).head(3))
                                      .reset_index(drop=True))
top_products_per_warehouse_quarter.set_index([ 'cppp'], inplace=True)
# 显示结果
print(top_products_per_warehouse_quarter)

# 将结果保存到CSV文件
top_products_per_warehouse_quarter.to_csv('top_cppp.csv')